const service = require('./myFocusService.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataSource: null,
    items: [],
    hasNextPage: false,
    startPage: 0,  //开始页
    nextPage: 1,
    loadMoreData: '加载更多……',
    isLoading: true,
  },
  
  onLoad: function (options) {
  },
  setSourceData: function(){
    var that = this
    var parma = {
      userId: getApp().globalData.userId,
      startPage: that.data.startPage
    }
    service.requestProducts(parma, function (data) {
      var array = data.items
      that.setData({
        startPage: data.nextPage
      })
      that.setData({ dataSource: data, items: that.data.items.concat(array), hasNextPage: data.hasNextPage ,isLoading: false});
    });
  },
  onShow: function (options) {
    this.setData({
       items: [],
       startPage: 0
    });
    this.setSourceData()
  },
  /**
 * view触底监听
 */
  onReachBottom: function () {
    var self = this;
    // 当前页是最后一页
    if (!self.data.hasNextPage) {
      self.setData({
        loadMoreData: '暂无更多数据'
      })
      return;
    }
    setTimeout(function () {
      console.log('上拉加载更多');
      self.setData({
        hideBottom: false
      })
      self.setSourceData();
    }, 300);
  },
  //item点击事件
  itemClick: function(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../homepage/personalhomepage?userId=' + e.currentTarget.dataset.id.userId,
    })
  }

})
